package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.OssUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 图片 web层
 *
 * @author owen
 */
@Validated
@CrossOrigin
@ResponseBody
@RestController
@RequestMapping("/photo")
public class PhotoController {

    /**
     * showdoc
     *
     * @param file 必选 file 图片
     * @param type 必选 string 类型(1.用户管理 2:意见反馈3:土地图片;4:种子图片;5:收成图片6土地类型 7农田服务)
     * @return {"flag": true,"code":20000,"message": "上传成功","data": "note/1c627bd9-66c4-4c37-b114-be42e9a669b8.jpg"}
     * @catalog 图片
     * @title 图片上传
     * @description
     * @method get
     * @url /photo/upload
     * @return_param flag boolean 是否成功
     * @return_param code int 返回码
     * @return_param message string 返回信息
     */
    @RequestMapping("/upload")
    public AjaxResult upload(@NotNull MultipartFile file,
                         @NotBlank String type) throws IOException {
        String filename = file.getOriginalFilename();
        int idx = filename.lastIndexOf(".");
        String uuidStr = UUID.randomUUID().toString() + filename.substring(idx);
        if ("1".equals(type)) {//用户管理
            filename = "tx/HT/" + uuidStr;
        } else if ("2".equals(type)) {
            filename = "suggest/HT/" + uuidStr;
        } else if ("3".equals(type)) {//土地管理
            filename = "TDGL/HT/" + uuidStr;
        } else if ("4".equals(type)) {//种子管理
            filename = "ZZ/HT/" + uuidStr;
        }  else if ("5".equals(type)) {//收成管理
            filename = "SC/HT/" + uuidStr;
        } else if ("6".equals(type)) {//土地类型管理
            filename = "TDLX/HT/" + uuidStr;
        } else if ("7".equals(type)) {//农田服务
            filename = "TDLX/HT/" + uuidStr;
        } else {
            throw new RuntimeException("用户:---上传图片参数异常");
        }
        OssUtil.uploadToOss(filename, file.getInputStream());
        return new AjaxResult( 200, "上传成功", filename);
    }




   /* // 文件类型
    public static String IMG_TYPE_PNG = "PNG";
    public static String IMG_TYPE_JPG = "JPG";
    public static String IMG_TYPE_JPEG = "JPEG";
    public static String IMG_TYPE_DMG = "BMP";
    public static String IMG_TYPE_GIF = "GIF";
    public static String IMG_TYPE_SVG = "SVG";

    @RequestMapping("/upload")
    public Result upload(MultipartFile imageFile) throws IOException {
        Map<String,Object> map = new HashMap<>();
        if(imageFile == null ){
            System.out.println("imageFile不能为空");
        }
        String thumbnailPathName = "D:\\aaa\\"+imageFile.getOriginalFilename();
        //如果图片大小超过1M，进行压缩
        String suffix = imageFile.getOriginalFilename()
                .substring(imageFile.getOriginalFilename().lastIndexOf(".") + 1);
        if ((IMG_TYPE_DMG.equals(suffix.toUpperCase())
                || IMG_TYPE_GIF.equals(suffix.toUpperCase())
                || IMG_TYPE_JPEG.equals(suffix.toUpperCase())
                || IMG_TYPE_JPG.equals(suffix.toUpperCase())
                || IMG_TYPE_PNG.equals(suffix.toUpperCase())
                || IMG_TYPE_SVG.equals(suffix.toUpperCase()))) {
            if (imageFile.getSize() > 1*1024*1024) {
                Thumbnails.of(imageFile.getInputStream()).scale(1f).outputQuality(0.5f)
                        .toFile(thumbnailPathName);
            }
        }
        map.put("thumbnailUrl", thumbnailPathName);
        return new Result(true, StatusCode.OK, "上传成功", map);
    }*/

}
